package tenant

import (
	"context"
	constants2 "git.mycaigou.com/gfyx/micro-gfyx-user-service/infrastructure/common/constants"
	"git.mycaigou.com/gfyx/micro-gfyx-user-service/infrastructure/repository/common"
	"gorm.io/gorm"
	"time"
)

type TenantsProfitRepository struct {
	common.TenantServiceDB
}

func NewTenantsProfitRepository() TenantsProfitRepositoryIface {
	return &TenantsProfitRepository{}
}

func (g *TenantsProfitRepository) AddTenantsProfit(ctx context.Context, data *TenantsProfit) (int32, error) {
	data.CreatedOn = time.Now()
	data.ModifiedOn = time.Now()
	err := g.GetTenantServiceDB(ctx).Create(&data).Error
	if err != nil {
		return 0, err
	}
	return data.Id, nil
}

// AddTenantsProfitBatch 批量插入
func (g *TenantsProfitRepository) AddTenantsProfitBatch(ctx context.Context, data []*TenantsProfit) error {
	for _, value := range data {
		value.CreatedOn = time.Now()
		value.ModifiedOn = time.Now()
	}
	err := g.GetTenantServiceDB(ctx).CreateInBatches(data, len(data)).Error
	if err != nil {
		return err
	}
	return nil
}

func (g *TenantsProfitRepository) SaveTenantsProfit(ctx context.Context, data *TenantsProfit) error {
	data.ModifiedOn = time.Now()
	err := g.GetTenantServiceDB(ctx).Save(data).Error
	if err != nil {
		return err
	}
	return nil
}

func (g *TenantsProfitRepository) GetTenantsProfit(ctx context.Context, tenantCode string) ([]*TenantsProfit, error) {
	tenantsProfit := make([]*TenantsProfit, 0)
	err := g.GetTenantServiceDB(ctx).
		Table(TenantsProfit{}.TableName()).
		Where("is_deleted = ?", constants2.IsDeletedFalse).
		Where("tenant_code = ?", tenantCode).
		Order("created_on desc").Find(&tenantsProfit).Error
	if err != nil && err != gorm.ErrRecordNotFound {
		return tenantsProfit, err
	}
	return tenantsProfit, nil
}

func (g *TenantsProfitRepository) ExistTenantProfit(ctx context.Context, deliveryNo string, deliveryBatchNo string, deliveryAuthId string) (*TenantsProfit, error) {
	tenantsProfit := &TenantsProfit{}
	err := g.GetTenantServiceDB(ctx).
		Table(TenantsProfit{}.TableName()).
		Where("is_deleted = ?", constants2.IsDeletedFalse).
		Where("delivery_batch_no = ?", deliveryBatchNo).
		Where("delivery_auth_id = ?", deliveryAuthId).
		Where("delivery_no = ?", deliveryNo).
		Order("created_on desc").First(tenantsProfit).Error

	if err != nil && err != gorm.ErrRecordNotFound {
		return tenantsProfit, err
	}
	return tenantsProfit, nil
}

func (g *TenantsProfitRepository) GetLatestProfitByCode(ctx context.Context, deliveryNo string, profitCode string, tenantCode string) (*TenantsProfit, error) {
	tenantsProfit := &TenantsProfit{}
	err := g.GetTenantServiceDB(ctx).
		Table(TenantsProfit{}.TableName()).
		Where("is_deleted = ?", constants2.IsDeletedFalse).
		Where("profit_code = ?", profitCode).
		Where("tenant_code = ?", tenantCode).
		Where("delivery_no = ?", deliveryNo).
		Order("end_time desc").First(tenantsProfit).Error
	if err != nil && err != gorm.ErrRecordNotFound {
		return tenantsProfit, err
	}
	return tenantsProfit, nil
}

// GetTenantClueRadarProfitList 获取线索雷达租户权益列表
func (g *TenantsProfitRepository) GetTenantClueRadarProfitList(ctx context.Context, tenantCode string) (profitList []*TenantsProfit, err error) {
	// 线索雷达省份版、全国版 todo 配置到apollo
	productCodes := []string{"ClueRadarProvince", "ClueRadarNationwide"}
	var commonTenantProfitList []*TenantsProfit
	err = g.GetTenantServiceDB(ctx).Table(TenantsProfit{}.TableName()).Where("tenant_code = ? and profit_code in ?", tenantCode, productCodes).Order("end_time desc").Limit(2).Find(&commonTenantProfitList).Error
	if err != nil {
		return nil, err
	}
	return commonTenantProfitList, nil
}

func (g *TenantsProfitRepository) GetTenantProfitCount(ctx context.Context, tenantCode string) (count int64, err error) {
	count = 0
	err = g.GetTenantServiceDB(ctx).Table(TenantsProfit{}.TableName()).Where("tenant_code = ?", tenantCode).Count(&count).Error
	if err != nil {
		return 0, err
	}
	return count, nil
}
